package com.rimi.project.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.rimi.project.bean.User;
import com.rimi.project.dao.UserDao;
import com.rimi.project.util.JDBCutil;

public class UserDaoImpl implements UserDao{
	
	// 通过账号密码查询
	@Override
	public User findUserByAccAndPassword(String userName, String password) {
		
		String sql = "select * from user where acc = ? and password = ? limit 1";
		
		ResultSet resultSet = JDBCutil.preQuery(sql, new String[] {userName,password});
		
		User user = null;
		
		try {
			while(resultSet.next()) {
				user = new User(resultSet.getString("acc"), 
						resultSet.getString("password"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return user;
	}
	
	// 通过账号查询
	@Override
	public User findUserByAcc(String userName) {
		String sql = "select * from user where acc = ? limit 1";
		
		ResultSet resultSet = JDBCutil.preQuery(sql, new String[] {userName});
		
		User user = null;
		
		try {
			while(resultSet.next()) {
				user = new User(resultSet.getString("acc"), 
						resultSet.getString("password"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return user;
	}
	
	// 添加账号密码
	@Override
	public boolean insertUser(String userName, String password) {
		String sql = "insert into user values(?,?)";
		
		int row = JDBCutil.preUpdate(sql, new String[] {userName,password});
		
		return row == 1;
	}

}
